package com.fone.player.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.personal.CacheActivity;
import com.fone.player.activity.personal.NetCollectionActivity;
import com.fone.player.constant.FoneConstant;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;
import com.fone.player.util.SystemUtil;

public class ChangeTempLimitDialog implements OnClickListener {

	private Button changeSureBtn, changeCancelBtn;
	private RadioGroup mLimitRg;
	private CustomDialog dialog;
	private Context mContext;
	private Handler mHandler;
	private RadioButton mLimitItem1, mLimitItem2, mLimitItem3, mLimitItem4;
	private long baseItem;
	private int checkedId = SharedPreferenceModule.getInstance().getInt(
			FoneConstant.TMP_LIMIT_CHOOSED_ID_FLAG_SP, R.id.rt_limit_item1);
	private long selectedSize;

	public ChangeTempLimitDialog(Context mContext,
			NetCollectionActivity activity, Handler mHandler) {

		this.mContext = mContext;
		this.mHandler = mHandler;

		View changeTmpLimitView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_change_tmp_limit_dialog, null);
		dialog = new CustomDialog((Context) activity, changeTmpLimitView);
		dialog.show();
		changeTmpLimitView.setMinimumWidth((int) (ScreenUtil
				.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度

		initView(changeTmpLimitView);
	}

	public ChangeTempLimitDialog(Context mContext, CacheActivity activity,
			Handler mHandler) {
		this.mContext = mContext;
		this.mHandler = mHandler;
		// mSp = mContext.getSharedPreferences(FoneConstant.FONE_SP_NAME_SP,
		// Activity.MODE_PRIVATE);
		View changeTmpLimitView = LayoutInflater.from(mContext).inflate(
				R.layout.personal_change_tmp_limit_dialog, null);
		dialog = new CustomDialog(activity, changeTmpLimitView);
		dialog.show();

		changeTmpLimitView.setMinimumWidth((int) (ScreenUtil
				.getScreenWidthPix(mContext) * 0.8));// 设置dialog的宽度

		initView(changeTmpLimitView);
	}

	private void initView(View changeTmpLimitView) {

		mLimitRg = (RadioGroup) changeTmpLimitView
				.findViewById(R.id.dialog_limit_rg);
		changeSureBtn = (Button) changeTmpLimitView
				.findViewById(R.id.limit_sure_id);
		changeCancelBtn = (Button) changeTmpLimitView
				.findViewById(R.id.limit_cancel_id);

		mLimitItem1 = (RadioButton) changeTmpLimitView
				.findViewById(R.id.rt_limit_item1);
		mLimitItem2 = (RadioButton) changeTmpLimitView
				.findViewById(R.id.rt_limit_item2);
		mLimitItem3 = (RadioButton) changeTmpLimitView
				.findViewById(R.id.rt_limit_item3);
		mLimitItem4 = (RadioButton) changeTmpLimitView
				.findViewById(R.id.rt_limit_item4);
		// TODO 从SharePerfence中获取保存的值
		// mLimitRg.check(R.id.rt_limit_item1);
		L.v("测试",
				"SP选中的id:"
						+ SharedPreferenceModule.getInstance().getInt(
								FoneConstant.TMP_LIMIT_CHOOSED_ID_FLAG_SP,
								R.id.rt_limit_item1));
		mLimitRg.check(SharedPreferenceModule.getInstance().getInt(
				FoneConstant.TMP_LIMIT_CHOOSED_ID_FLAG_SP, R.id.rt_limit_item1));

		switch (mLimitRg.getCheckedRadioButtonId()) {
		case R.id.rt_limit_item1:
			selectedSize = baseItem * 1;
			break;
		case R.id.rt_limit_item2:
			selectedSize = baseItem * 4;
			break;
		case R.id.rt_limit_item3:
			selectedSize = baseItem * 6;
			break;
		case R.id.rt_limit_item4:
			selectedSize = baseItem * 8;
			break;
		}

		mLimitRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {

				ChangeTempLimitDialog.this.checkedId = checkedId;

				switch (checkedId) {
				case R.id.rt_limit_item1:
					selectedSize = baseItem * 1;
					break;
				case R.id.rt_limit_item2:
					selectedSize = baseItem * 4;
					break;
				case R.id.rt_limit_item3:
					selectedSize = baseItem * 6;
					break;
				case R.id.rt_limit_item4:
					selectedSize = baseItem * 8;
					break;
				}

				L.v("测试", "选中的id:" + checkedId);

			}
		});

		changeSureBtn.setOnClickListener(this);
		changeCancelBtn.setOnClickListener(this);

		initStorageOption();
	}

	/**
	 * 初始化存储空间选项(条目大小)
	 * 
	 * @return void
	 * @throws
	 */

	private void initStorageOption() {

		if (SystemUtil.getInstance().isSDCardMounted()) {
			baseItem = SystemUtil.getInstance().getTotalSpaceByteBySDCard() / 16;
		} else {
			baseItem = SystemUtil.getInstance().getTotalSpaceByteByPhoneBody() / 16;

		}
		mLimitItem1.setText(Formatter.formatFileSize(mContext, baseItem * 1));
		mLimitItem2.setText(Formatter.formatFileSize(mContext, baseItem * 4));
		mLimitItem3.setText(Formatter.formatFileSize(mContext, baseItem * 6));
		mLimitItem4.setText(Formatter.formatFileSize(mContext, baseItem * 8));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.limit_sure_id:
			// 确定按钮

			// 如果当前选中限制容量小于实际容量 不允许修改
			long currentFreeSize = SystemUtil.getInstance().isSDCardMounted() ? SystemUtil
					.getInstance().getFreeSpaceByteBySDCard() : SystemUtil
					.getInstance().getFreeSpaceByteByPhoneBody();
			L.v("JasonZue", "selectedSize: " + selectedSize
					+ "currentFreeSize: " + currentFreeSize);
			if (currentFreeSize < selectedSize) {
				Toast.makeText(mContext, "您当前选中的可用容量已经高于实际可用值",
						Toast.LENGTH_LONG).show();
				return;
			}

			SharedPreferenceModule.getInstance().setInt(
					FoneConstant.TMP_LIMIT_CHOOSED_ID_FLAG_SP, checkedId);

			Message msg = new Message();
			msg.what = NetCollectionActivity.UPDATE_LIMIT_TEMP_TEXT;
			switch (mLimitRg.getCheckedRadioButtonId()) {
			case R.id.rt_limit_item1:
				SharedPreferenceModule.getInstance().setString(
						FoneConstant.TMP_LIMIT_ITEM_LONG_FLAG_SP,
						Formatter.formatFileSize(mContext, baseItem * 1));
				SharedPreferenceModule.getInstance().setLong(
						FoneConstant.TMP_LIMIT_SIZE, baseItem * 1);
				break;
			case R.id.rt_limit_item2:
				SharedPreferenceModule.getInstance().setString(
						FoneConstant.TMP_LIMIT_ITEM_LONG_FLAG_SP,
						Formatter.formatFileSize(mContext, baseItem * 4));
				SharedPreferenceModule.getInstance().setLong(
						FoneConstant.TMP_LIMIT_SIZE, baseItem * 4);
				break;
			case R.id.rt_limit_item3:
				SharedPreferenceModule.getInstance().setString(
						FoneConstant.TMP_LIMIT_ITEM_LONG_FLAG_SP,
						Formatter.formatFileSize(mContext, baseItem * 6));
				SharedPreferenceModule.getInstance().setLong(
						FoneConstant.TMP_LIMIT_SIZE, baseItem * 6);
				break;
			case R.id.rt_limit_item4:
				SharedPreferenceModule.getInstance().setString(
						FoneConstant.TMP_LIMIT_ITEM_LONG_FLAG_SP,
						Formatter.formatFileSize(mContext, baseItem * 8));
				SharedPreferenceModule.getInstance().setLong(
						FoneConstant.TMP_LIMIT_SIZE, baseItem * 8);
				break;
			}
			mHandler.sendMessage(msg);
			dialog.cancel();
			break;
		case R.id.limit_cancel_id:
			// 取消按钮
			dialog.cancel();
			break;
		}

	}
}
